package com.kws.merchant.mapper;

import com.kws.merchant.domain.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kws.merchant.domain.vo.ItemStatisticsVo;
import com.kws.merchant.domain.vo.MicrogridOrder;
import com.kws.merchant.domain.vo.MicrogridSale;
import com.kws.merchant.domain.vo.OrderRankVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * 订单(Order)表数据库访问层
 *
 * @author yxy
 * @since 2024-10-22 17:21:07
 */

public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 根据时间查询订单数量
     * @param userId
     * @param time
     * @return
     */
    @Select("select  count(DISTINCT order_no) from tb_order where merchant_id='${userId}' and DATE(create_time) like '${time}'")
    Double listOrderByTime(@Param("userId") Long userId,@Param("time") String time);

    /**
     *根据时间查询支付买家数
     * @param userId
     * @param time
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_order where merchant_id='${userId}' and status in (1,2,3,4,6) and DATE(create_time) like '${time}'")
    Double listOrderPayByTime(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 支付金额
     * @param userId
     * @param time
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${userId}' and status in (1,2,3,4,6) and DATE(create_time) like '${time}'")
    Double merchantPayMoneyByTime(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 统计近30天内支付金额排行
     * @param companyId
     * @param date
     * @param beforeDate
     * @return
     */
    @Select("select ROW_NUMBER() OVER (ORDER BY sum(o.order_price) DESC) as serial,o.user_id,u.nick_name,IFNULL(sum(o.order_price),0) as price from tb_order o left join sys_user u on o.user_id=u.user_id where o.merchant_id=\"${companyId}\" and o.type=${type} and o.create_time BETWEEN \"${beforeDate}\" AND \"${date}\" GROUP BY user_id ORDER BY price desc limit 0,10")
    List<OrderRankVo> payRanking(@Param("companyId") Long companyId,@Param("date") String date,@Param("beforeDate") String beforeDate,@Param("type") Integer type);

    /**
     * 根据类型时间查询支付金额
     * @param companyId
     * @param time
     * @param type
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${companyId}' and type=${type} and status in (1,2,3,4,6) and DATE(create_time) like '${time}'")
    int merchantPayMoneyByTimeByType( @Param("companyId") Long companyId,@Param("time") String time,@Param("type") String type);

    /**
     * 根据类型时间查询支付买家数
     * @param companyId
     * @param time
     * @param type
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_order where merchant_id='${companyId}' and type=${type} and status in (1,2,3,4,6) and DATE(create_time) like '${time}'")
    int listOrderPayByTimeByType( @Param("companyId") Long companyId,@Param("time") String time,@Param("type") Integer type);

    /**
     * 根据类型时间查询支付订单数
     * @param companyId
     * @param time
     * @param type
     * @return
     */
    @Select("select  count(DISTINCT order_no) from tb_order where merchant_id='${companyId}' and type=${type} and DATE(create_time) like '${time}'")
    int payOrderNumByType( @Param("companyId") Long companyId,@Param("time") String time,@Param("type") Integer type);

    /**
     * 总的支付金额
     * @param userId
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${userId}' and status in (1,2,3,4,6)")
    Double countPayNum(@Param("userId")Long userId);

    /**
     * 总订单数
     * @param userId
     * @return
     */
    @Select("select  count(DISTINCT order_no) from tb_order where merchant_id='${userId}'")
    Double countOrder(@Param("userId")Long userId);

    /**
     * 总支付买家数
     * @param userId
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_order where merchant_id='${userId}' and status in (1,2,3,4,6)")
    Double countPayOrder(@Param("userId") Long userId);

    /**
     * 根据年月查询支付金额
     * @param companyId
     * @param year
     * @param month
     * @param type
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${companyId}' and type='${type}' and YEAR(create_time)='${year}' and MONTH(create_time)='${month}' and status in (1,2,3,4,6);")
    Double countPayByYearAndMonth(@Param("companyId") Long companyId,@Param("year") Integer year, @Param("month") Integer month,@Param("type") Integer type);

    /**
     * 根据年月查询退款金额
     * @param companyId
     * @param year
     * @param month
     * @param type
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${companyId}' and type='${type}' and YEAR(create_time)='${year}' and MONTH(create_time)='${month}' and status in (7);")
    Double refundPayByYearAndMonth(@Param("companyId") Long companyId, @Param("year")Integer year, @Param("month") Integer month,@Param("type") Integer type);

    /**
     * 根据sql查询支付金额
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${companyId}' and type='${type}' and status in (1,2,3,4,6) ${sql};")
    Double countPayBySql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);

    /**
     * 根据sql查询退款金额
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  count(order_price) from tb_order where merchant_id='${companyId}' and type='${type}' and status in (7) ${sql};")
    Double refundPayBySql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);

    /**
     * 支付买家数
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_order where merchant_id='${companyId}' and type='${type}' and status in (1,2,3,4,6) ${sql};")
    Double countPayPeopleBySql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);

    /**
     * 支付订单数
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  count(*) from tb_order where merchant_id='${companyId}' and type='${type}' and status in (1,2,3,4,6) ${sql};")
    Double countPayOrderBySql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);

    @Select("select serial from (select ROW_NUMBER() OVER () as serial,o.merchant_id,u.company_name,IFNULL(sum(o.order_price),0) as price from tb_order o left join tb_merchant u on o.merchant_id=u.id \n" +
            "where  o.type=${type} and o.create_time BETWEEN \"${beforeDate}\" AND \"${date}\" GROUP BY merchant_id ORDER BY price desc)  t where t.merchant_id=${companyId}")
    Integer merchantPayRankingByOne(@Param("companyId") Long companyId,@Param("date") String date,@Param("beforeDate") String beforeDate,@Param("type") Integer type);

    /**
     * 统计订单销售数量
     * @param id
     * @return
     */
    @Select("select sum(goods_count) from tb_order where goods_id=\"${id}\" and status in (1,2,3,4,6) ;")
    Integer countByGoodsId(@Param("id") Long id);

    @Select("select  count(order_price) from tb_order where merchant_id='${companyId}'  and status in (1,2,3,4,6) ${sql};")
    Double countPayBySqlType(@Param("companyId") Long companyId,@Param("sql") String sql);

    /**
     * 按项目类型统计销售额
     * @param companyId
     * @param sql
     */
    @Select("select project_type,count(order_price) as order_price from tb_order where merchant_id='${companyId}' ${sql} GROUP BY project_type ; ")
    List<MicrogridSale> countPayBySqlGroupByProjectType(@Param("companyId")Long companyId, @Param("sql") String sql);

    /**
     * 微电网-销售量、销售额统计
     * @param sql
     * @return
     */
    @Select("select count(*) as orderNum,count(order_price) as orderMoney from tb_order where merchant_id='${companyId}' and status in (1,2,3,4,6) ${sql} ;")
    ItemStatisticsVo itemStatistics(@Param("companyId")Long companyId,@Param("sql") String sql);

    /**
     * 统计总的金额
     * @param companyId
     * @param sql
     * @return
     */
    @Select("select  count(*) from tb_order where merchant_id='${companyId}'  ${sql};")
    Integer countOrderPrice(@Param("companyId")Long companyId, @Param("sql") String sql);

    /**
     * 按订单类型统计销售额
     * @param companyId
     * @param sql
     * @return
     */
    @Select("select status,count(*) as orderNum from tb_order where merchant_id='${companyId}' ${sql} GROUP BY status ; ")
    List<MicrogridOrder> countPayBySqlGroupByOrderStatus(@Param("companyId")Long companyId, @Param("sql") String sql);
}
